package com.example.skinhelper;

import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import java.util.Arrays;
import java.util.List;

public class DrawViewSetting implements ViewSetting{

    List<String> list= Arrays.asList(new String[]{
            "background",
            "src",
    });

    @Override
    public List<String> getAttrNameList() {
        return list;
    }

    @Override
    public void setSkin(View view, String attrName, String valueId, Resources resources) {
        Log.d("DrawViewSetting", "setSkin: attrName="+attrName+" valueId="+valueId);

        if (valueId.charAt(0)!='@'){
            return;
        }
        Drawable drawable=null;
        try {
            Integer integer = Integer.valueOf(valueId.substring(1));
            if (IdUtils.findResById(integer).getType().equals("drawable")) {
                drawable = resources.getDrawable(integer);
            }
        }catch (Exception e){
            return;
        }

        if (drawable==null){
            return;
        }
        switch (attrName){
            case "background":
                view.setBackground(drawable);
                break;
            case "src":
                if (view instanceof ImageView){
                    ((ImageView) view).setImageDrawable(drawable);
                }
                break;
        }
    }
}
